package stax;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.InputStream;
import java.util.Iterator;

public class TestStAXParser {

    public static void main(String[] args) {

        XMLInputFactory factory=XMLInputFactory.newInstance();


        InputStream in=TestStAXParser.class
                                     .getClassLoader()
                                     .getResourceAsStream("students.xml");


        try {
            //解析器
            XMLEventReader parser=factory.createXMLEventReader(in);


            while(parser.hasNext()){
                XMLEvent event=parser.nextEvent();
                // System.out.println(event);

                if(event.isStartElement()){
                    StartElement startElement=event.asStartElement();

                    // System.out.println(startElement.getName());

                    Iterator<Attribute> attributeIterator=startElement.getAttributes();

                    while(attributeIterator.hasNext()){
                        Attribute attribute=attributeIterator.next();
                        // System.out.println(attribute.getName()+"="+attribute.getValue());
                    }


                }

                if(event.isCharacters()){
                    System.out.println(event.asCharacters());
                }


            }















        } catch (XMLStreamException e) {
            e.printStackTrace();
        }


    }
}
